/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui.misc;

import acousticfield3d.gui.MainForm;
import acousticfield3d.math.M;
import acousticfield3d.math.Ray;
import acousticfield3d.math.Vector3f;
import acousticfield3d.scene.Entity;
import acousticfield3d.simulation.ControlPoint;
import acousticfield3d.simulation.Transducer;
import acousticfield3d.utils.FileUtils;
import acousticfield3d.utils.Parse;
import java.util.List;

/**
 *
 * @author am14010
 */
public class MetaSurfaces extends javax.swing.JFrame {
    final MainForm mf;
    
    public MetaSurfaces(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        heightsMetaButton = new javax.swing.JButton();
        bowlMetaButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        distanceLensButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        heightsMetaButton.setText("Heights Meta");
        heightsMetaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                heightsMetaButtonActionPerformed(evt);
            }
        });

        bowlMetaButton.setText("Bowl Meta");
        bowlMetaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bowlMetaButtonActionPerformed(evt);
            }
        });

        jButton1.setText("AddCP");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("AddCP height");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        distanceLensButton.setText("Distance Lens");
        distanceLensButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distanceLensButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(heightsMetaButton)
                                .addGap(18, 18, 18)
                                .addComponent(bowlMetaButton))
                            .addComponent(distanceLensButton))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(heightsMetaButton)
                    .addComponent(bowlMetaButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addComponent(distanceLensButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void heightsMetaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_heightsMetaButtonActionPerformed
        final List<Transducer> trans = mf.simulation.getTransducers();
        
        final Entity e = mf.movePanel.getBeadEntity();
        if (e == null) { return; }
        
        final Vector3f tPos = e.getTransform().getTranslation();
        
        for(Transducer t : trans){
            final Vector3f pos = t.getTransform().getTranslation();
            final float phase = t.getPhase();
            final float wavelength = mf.simulation.getMediumSpeed() / t.getFrequency();
            final float incDist = phase * wavelength / 2;
            final float currentDistance = tPos.distance( pos );
            final float targetDist = currentDistance + incDist;
            final float lateralDist = M.sqrt( M.sqr( tPos.x - pos.x ) + M.sqr( tPos.z - pos.z ));
            
            final float depth = M.sqrt( targetDist*targetDist - lateralDist*lateralDist);
            pos.y = tPos.y - depth; 
        }
        mf.needUpdate();
    }//GEN-LAST:event_heightsMetaButtonActionPerformed

    private void bowlMetaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bowlMetaButtonActionPerformed
        final List<Transducer> trans = mf.simulation.getTransducers();
        
        final Entity e = mf.movePanel.getBeadEntity();
        if (e == null) { return; }
        
        final Vector3f tPos = e.getTransform().getTranslation();
        
        float maxDistance = 0;
        for(Transducer t : trans){
            maxDistance = M.max( maxDistance,  t.getTransform().getTranslation().distance(tPos));
        }
        
        for(Transducer t : trans){
            final Vector3f pos = t.getTransform().getTranslation();
            final Ray r = new Ray(tPos, pos);
            pos.set( r.pointAtDistance(maxDistance) );
        }
        
        mf.needUpdate();
    }//GEN-LAST:event_bowlMetaButtonActionPerformed

    
    private void addTransducersMockUps(boolean addHeight){
        final List<Transducer> trans = mf.simulation.getTransducers();
        for(Transducer t : trans){
            final Vector3f pos = t.getTransform().getTranslation();
            ControlPoint cp = new ControlPoint(t.getMesh(), null, t.getShader());
            cp.getMaterial().set( t.getMaterial() );
            cp.setColor( Gradients.get().getGradientAmpAndPhase(1, 0) );
            cp.getTransform().set( t.getTransform() );
            if (addHeight){
                final float phase = t.getPhase();
                final float wavelength = mf.simulation.getMediumSpeed() / t.getFrequency();
                final float incDist = phase * wavelength / 2;
                cp.getTransform().getTranslation().y -= incDist;
            }
            mf.scene.getEntities().add(cp);
        }
        mf.needUpdate();
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addTransducersMockUps(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       addTransducersMockUps(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void distanceLensButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distanceLensButtonActionPerformed
        //get first select particle
        final Entity e = mf.movePanel.getBeadEntity();
        if(e == null) {return;}
        
        final Vector3f ePos = e.getTransform().getTranslation();
        
        //iterate over the transducers, get the maximum and minimum distance
        float maxDistance = 0;
        for(Transducer t : mf.simulation.transducers){
            maxDistance = M.max(maxDistance, t.getTransform().getTranslation().distance( ePos ) );
        }

        //displace them on y the different between the max
        for(Transducer t : mf.simulation.transducers){
            final Vector3f tPos = t.getTransform().getTranslation();
            final float targetDist = maxDistance;    
            final float lateralDist = M.sqrt( M.sqr( tPos.x - ePos.x ) + M.sqr( tPos.z - ePos.z ));
            final float depth = M.sqrt( targetDist*targetDist - lateralDist*lateralDist);
            tPos.y = ePos.y - depth; 
        }
        mf.needUpdate();
    }//GEN-LAST:event_distanceLensButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bowlMetaButton;
    private javax.swing.JButton distanceLensButton;
    private javax.swing.JButton heightsMetaButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
}
